package Ventanas.admin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import util.GetFrame;
import BaseDeDatos.ConsultasUsuario;
import ClasesBasicas.Usuario;

public class PanelModificar extends JPanel implements ActionListener {
	private javax.swing.JButton bAtras;
	private javax.swing.JButton bModificar;
	private javax.swing.JLabel lEmail;
	private javax.swing.JLabel lId;
	private javax.swing.JLabel lNombre;
	private javax.swing.JLabel lPsw;
	private javax.swing.JLabel lTlf;
	private javax.swing.JTextField tfEmail;
	private javax.swing.JTextField tfId;
	private javax.swing.JTextField tfNombre;
	private javax.swing.JTextField tfPsw;
	private javax.swing.JTextField tfTlf;
	private JLabel fondo;
	private PanelInicialA inicial;
	private Usuario user;

	public PanelModificar(PanelInicialA inicial, Usuario user) {
		this.user = user;
		this.inicial = inicial;
		lId = new javax.swing.JLabel();
		lNombre = new javax.swing.JLabel();
		lPsw = new javax.swing.JLabel();
		lTlf = new javax.swing.JLabel();
		lEmail = new javax.swing.JLabel();
		tfId = new javax.swing.JTextField();
		tfId.setEditable(false);
		tfNombre = new javax.swing.JTextField();
		tfPsw = new javax.swing.JTextField();
		tfTlf = new javax.swing.JTextField();
		tfEmail = new javax.swing.JTextField();
		bModificar = new javax.swing.JButton();
		bAtras = new javax.swing.JButton();
		fondo = new JLabel();

		setLayout(null);

		lId.setText("Id usuario");
		lId.setForeground(new java.awt.Color(255, 255, 255));
		add(lId);
		lId.setBounds(220, 100, 100, 30);

		lNombre.setText("Nombre");
		lNombre.setForeground(new java.awt.Color(255, 255, 255));
		add(lNombre);
		lNombre.setBounds(220, 150, 100, 30);

		lPsw.setText("Password");
		lPsw.setForeground(new java.awt.Color(255, 255, 255));
		add(lPsw);
		lPsw.setBounds(220, 210, 100, 30);

		lTlf.setText("Telefono");
		lTlf.setForeground(new java.awt.Color(255, 255, 255));
		add(lTlf);
		lTlf.setBounds(220, 270, 100, 30);

		lEmail.setText("Email");
		lEmail.setForeground(new java.awt.Color(255, 255, 255));
		add(lEmail);
		lEmail.setBounds(220, 330, 100, 30);
		add(tfId);
		tfId.setBounds(320, 100, 160, 30);
		add(tfNombre);
		tfNombre.setBounds(320, 150, 160, 30);
		add(tfPsw);
		tfPsw.setBounds(320, 210, 160, 30);
		add(tfTlf);
		tfTlf.setBounds(320, 270, 160, 30);
		add(tfEmail);
		tfEmail.setBounds(320, 330, 160, 30);

		bModificar.setText("Editar");
		add(bModificar);
		bModificar.setBounds(350, 410, 80, 30);
		bModificar.addActionListener(this);

		bAtras.setText("Atras");
		add(bAtras);
		bAtras.setBounds(90, 500, 70, 30);
		bAtras.addActionListener(this);

		tfEmail.setText(user.getEmail());
		tfId.setText(user.getId_usuario());
		tfNombre.setText(user.getNombre());
		tfPsw.setText(user.getPassword());
		tfTlf.setText(user.getTelefono() + "");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(bModificar)) {
			if (tfEmail.getText().isEmpty() || tfId.getText().isEmpty()
					|| tfNombre.getText().isEmpty()
					|| tfPsw.getText().isEmpty() || tfTlf.getText().isEmpty()) {
				JOptionPane.showMessageDialog(null,
						"Por favor, rellene todos los campos",
						"Error al modificar usuario ",
						JOptionPane.ERROR_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(null,
						"Usuario: " + tfId.getText() + " modificado.",
						"Modificacion correcta! ", JOptionPane.INFORMATION_MESSAGE);
				
				ConsultasUsuario.modificarUsuario(tfId.getText(), tfPsw.getText(), tfNombre.getText(), Integer.parseInt(tfTlf.getText()), tfEmail.getText());
				VentanaA ventana = GetFrame.getVentanaA(this);
				this.setVisible(false);
				ventana.getContentPane().add(inicial);
				inicial.setVisible(true);
			}

		}
		if (e.getSource().equals(bAtras)) {
			VentanaA ventana = GetFrame.getVentanaA(this);
			this.setVisible(false);
			ventana.getContentPane().add(inicial);
			inicial.setVisible(true);
		}

	}
}
